﻿using System.Collections.Generic;
using UnityEngine;
using EchoFramework;

/// <summary>
/// 地图相关逻辑module
/// </summary>
public class MapLogicModule : LogicModuleBase
{
    public static MapLogicModule Instance
    {
        get
        {
            return LogicModuleManager.Instance.GetLogicModule<MapLogicModule>();
        }
    }

    /// <summary>
    /// 当前关卡
    /// </summary>
    public int Level { get; private set; }
    
    /// <summary>
    /// 当前关卡对应的地图点位配置表
    /// </summary>
    public List<MapPosCfg> LevelMapPosCfgList { get; private set; }

    /// <summary>
    /// 最大关卡
    /// </summary>
    public int MaxLevel { get; set; }
    
    /// <summary>
    /// 地图生成器
    /// </summary>
    public MapGenerator mapGenerator { get; set; }

    public override void OnInit()
    {
        base.OnInit();
        MaxLevel = MapPosCfg.GetMaxLevel();
    }

    /// <summary>
    /// 加载关卡地图
    /// </summary>
    /// <param name="level">关卡</param>
    public void LoadMapLevel(int level)
    {
        if (level > MaxLevel)
        {
            GameLogger.LogWarning($"关卡:{level} 不存在！！！");
            return;
        }
        
        PlayerLogicModule.Instnace.Init();
        LevelMapPosCfgList = MapPosCfg.GetMapCfgListByLevel(level);  //更新关卡配置信息
        if (LevelMapPosCfgList == null)
        {
            GameLogger.LogWarning($"关卡:{level} 不存在！！！");
            return;
        }
        
        if (mapGenerator == null)
        {
            GameObject mapGo = AssetManager.Instance.LoadGameObject("Assets/GameRes/GamePrefab/Map/MapGenerator.prefab");
            mapGenerator = mapGo.GetComponent<MapGenerator>();
        }
        mapGenerator.GenerateMap();
        Level = level;
        Coordinate playerInitCoor = MapPosCfg.GetPlayerInitCoor(level, out int initScore);
        PlayerLogicModule.Instnace.InitLevel(playerInitCoor, initScore, level);
        CameraManager.Instance.InitWithPlayer();
    }

    /// <summary>
    /// 返回当前关卡宽度
    /// </summary>
    public int GetLevelWidth(out int minX)
    {
        int minCoorX = int.MaxValue;
        int maxCoorX = int.MinValue;
        foreach (MapPosCfg mapPosCfg in LevelMapPosCfgList)
        {
            int coorX = mapPosCfg.PosCoordinate.X;
            minCoorX = coorX < minCoorX ? coorX : minCoorX;
            maxCoorX = coorX > maxCoorX ? coorX : maxCoorX;
        }

        minX = minCoorX;
        return maxCoorX - minCoorX + 1;
    }

    /// <summary>
    /// 返回当前关卡高度
    /// </summary>
    public int GetLevelHeight(out int minY)
    {
        int minCoorY = int.MaxValue;
        int maxCoorY = int.MinValue;
        foreach (MapPosCfg mapPosCfg in LevelMapPosCfgList)
        {
            int coorY = mapPosCfg.PosCoordinate.Y;
            minCoorY = coorY < minCoorY ? coorY : minCoorY;
            maxCoorY = coorY > maxCoorY ? coorY : maxCoorY;
        }

        minY = minCoorY;
        return maxCoorY - minCoorY + 1;
    }
}